import { Toast } from 'antd-mobile';
import pathToRegexp from 'path-to-regexp';
import { routerRedux } from 'dva/router';
import { Route } from 'dva/router';
import {
    upLoadVideoApi,
    publishGoods,
    authentication,
    delSomment,
    checkAuth,
    findSerach,
    findDetail,
    publishGoodsSearch,
    findSerachDetail,
    findSerachUserDetail,
    publishFind,
    styleApi,
    peopleApi,
    delOneSelf,
    selPeopleApi,
    fastFind,
    conListApi,
    myFansListApi,
    talkListApi,
    connerListApi,
    connerLike,
    commentLike,
    connerFollow,
    hotsListApi,
    personHome,
    findCommentList,
    submitSomment,
    commentDetail,
    fansList,
    othersHome,
    myArticleApi
} from "./api";
import {parse} from "query-string";
import {evaluateLike} from "../evaluate/api";

export default {
    namespace: 'findOthers',
    state: {
        pageNo:1,
        topicName:"",
        bought:"",
        cart:"",
        fav:"",
        viewed:"",
        hotType:"",
        keyWord:"",
        keyword:"",
        trendsType:"",
        findDetail: {},
        shopTrendsEntities:[],
        trendsConnerEntities:[],
        list:[],
        findSerachList:[],
        trendsFansEntities:[],
        trendsDetailEntity:[],
        trendsTopicEntities:[],
        trendsConnerImgEntities:[],
        personHome:{},
        trendsEntities: [],
        findSerachDetailList:[],
        findSerachUserDetailList:[],
        serachGoodsList:[],
        GoodsSerachList:[],
    },
    reducers: {
        save(state, {data}) {
            return {...state, ...data};
        },
        clearState() {
            return {list: [],
                shopTrendsEntities: [],
                personHome:{},
                findSerachDetailList:[],
                trendsDetailEntity:[],
                trendsTopicEntities:[],
                trendsConnerEntities:[],
                trendsConnerImgEntities:[],
                trendsFansEntities:[],
                trendsEntities: [],
                serachGoodsList:[],
                GoodsSerachList:[],
                findSerachUserDetailList:[]
            };
        }

    },
    effects: {
        *getStylesEFF({obj}, {put, call}) {
            console.log(obj)
            // Toast.loading('加载中...', 3);
            const result = yield call(styleApi, obj);
            if (result.code == 200) {
                if(result.msg==='获取成功'){
                    yield put({type: 'save', data: {styles:result.ret}});
                    return result
                    // Toast.hide();
                }else if(result.msg=='未查询到数据'){
                    Toast.info('暂无数据了');
                    const result = yield call(styleApi,{pageNo:1} );
                    yield put({type: 'save', data: {styles:result.ret}});
                    return result
                }
            } else {

            }
            // return {a: true};
            return result;
        },
        *findSerach({obj}, {put, call}) {
            const result = yield call(findSerach, obj);
            if (result.code == 200) {
                    yield put({type: 'save', data: {findSerachList:result.ret}});
                    return result
            }
            return result;
        },
        *findDetailsEFF({obj}, {put, call}) {
            const result = yield call(findDetail, obj);
            if (result.code == 200) {
                yield put({type: 'save', data: {trendsDetailEntity:result.ret.trendsDetailEntity}});
                return result
            }
            return result;
        },
        *upLoadVideoEFF({obj }, { put, call }) {
            Toast.loading('加载中...', 3);
            const result = yield call(upLoadVideoApi,obj);
            return result;
        },
        *authenticationEFF({obj }, { put, call }) {
            Toast.loading('加载中...', 3);
            const result = yield call(authentication,obj);
            return result;
        },
        *checkAuthEFF({obj }, { put, call }) {
            Toast.loading('加载中...', 3);
            const result = yield call(checkAuth,obj);
            return result;
        },
        *connerListEFF({ obj }, { put, call, select }) {
            //Toast.loading();
            console.log(obj)
            const findOthers = yield select((state)=>state.findOthers);
            const result = yield call(connerListApi, { topicName:findOthers.topicName, pageNo:findOthers.pageNo, ...obj });
            if (result.code == 200) {
                yield put({ type: 'save', data: result.ret  });
                if(result.ret==""){yield put({ type: 'save', data: { shopTrendsEntities: [],trendsConnerImgEntities:[]} })}else{
                    // if(result.pageNo <= 1) yield put({ type: 'save', data: {...result.ret, list: result.ret.list,pageSize:result.pageSize,pageNo:result.pageNo} });
                    if(result.ret.pageNo <= 1) yield put({ type: 'save', data: {...result.ret, shopTrendsEntities: result.ret.shopTrendsEntities,trendsConnerImgEntities:result.ret.trendsConnerImgEntities} });

                    else yield put({ type: 'save', data: { ...result.ret,shopTrendsEntities: [...findOthers.shopTrendsEntities, ...result.ret.shopTrendsEntities],trendsConnerImgEntities:result.ret.trendsConnerImgEntities } });

                }
            }
            return result;
        },
        *hotsListEFF({ obj }, { put, call, select }) {
            //Toast.loading();
            console.log(obj)
            const findOthers = yield select((state)=>state.findOthers);
            const result = yield call(hotsListApi, { hotType:findOthers.hotType, pageNo:findOthers.pageNo, ...obj });
            if (result.code == 200) {
                yield put({ type: 'save', data: result.ret  });
                if(result.ret==""){yield put({ type: 'save', data: { list: []} })}else{
                    // if(result.pageNo <= 1) yield put({ type: 'save', data: {...result.ret, list: result.ret.list,pageSize:result.pageSize,pageNo:result.pageNo} });
                    if(result.ret.pageNo <= 1) yield put({ type: 'save', data: {...result.ret, list: result.ret.list} });

                    else yield put({ type: 'save', data: { ...result.ret,list: [...findOthers.list, ...result.ret.list]} });

                    return { r:true };
                }
            }
        },
        *publishGoodsEFF({ obj }, { put, call, select }) {
            const findOthers = yield select((state)=>state.findOthers);
            const result = yield call(publishGoods, { bought:findOthers.bought,cart:findOthers.cart,fav:findOthers.fav,viewed:findOthers.viewed, pageNo:findOthers.pageNo, ...obj });
            if (result.code == 200) {
                yield put({ type: 'save', data: result.ret  });
                if(result.ret==""){yield put({ type: 'save', data: { serachGoodsList: []} })}else{
                    // if(result.pageNo <= 1) yield put({ type: 'save', data: {...result.ret, list: result.ret.list,pageSize:result.pageSize,pageNo:result.pageNo} });
                    if(result.ret.pageNo <= 1) yield put({ type: 'save', data: {...result.ret, serachGoodsList: result.ret.list} });

                    else yield put({ type: 'save', data: { ...result.ret,serachGoodsList: [...findOthers.serachGoodsList, ...result.ret.list]} });

                    return { r:true };
                }
            }
        },
        *publishGoodsSerachEFF({ obj }, { put, call, select }) {
            const findOthers = yield select((state)=>state.findOthers);
            const result = yield call(publishGoodsSearch, { keyword:findOthers.keyword,pageNo:findOthers.pageNo, ...obj });
            if (result.code == 200) {
                yield put({ type: 'save', data: result.ret  });
                if(result.ret==""){yield put({ type: 'save', data: { GoodsSerachList: []} })}else{
                    // if(result.pageNo <= 1) yield put({ type: 'save', data: {...result.ret, list: result.ret.list,pageSize:result.pageSize,pageNo:result.pageNo} });
                    if(result.ret.pageNo <= 1) yield put({ type: 'save', data: {...result.ret, GoodsSerachList: result.ret.list} });

                    else yield put({ type: 'save', data: { ...result.ret,GoodsSerachList: [...findOthers.GoodsSerachList, ...result.ret.list]} });

                    return { r:true };
                }
            }
        },
        *getPeopleEFF({obj}, {put, call}) {
            // Toast.loading('加载中...', 3);
            const result = yield call(peopleApi, obj);
            if (result.code == 200) {
                if(result.msg==='获取成功'){
                    yield put({type: 'save', data: {peoples:result.ret.list}});
                }
                // else if(result.msg=='未查询到数据'){
                //     Toast.info('暂无数据了');
                //     const result = yield call(peopleApi,{pageNo:1} );
                //     yield put({type: 'save', data: {peoples:result.ret.list}});
                //     return
                // }
            } else {

            }
            return {a: true}
        },
        *personHomeEFF({obj}, {put, call,select}) {
            const findOthers = yield select((state)=>state.findOthers);
            const result = yield call(personHome, {  pageNo:findOthers.pageNo, ...obj });
            if (result.code == 200) {
                yield put({ type: 'save', data: {personHome:result.ret}  });
                if(result.ret==""){yield put({ type: 'save', data: { trendsEntities: []} })}else{
                    // if(result.pageNo <= 1) yield put({ type: 'save', data: {...result.ret, list: result.ret.list,pageSize:result.pageSize,pageNo:result.pageNo} });
                    if(result.ret.trendsPersonHomeList.pageNo <= 1) yield put({ type: 'save', data: {personHome:result.ret, trendsEntities: result.ret.trendsPersonHomeList.trendsEntities} });

                    else yield put({ type: 'save', data: {personHome:result.ret,trendsEntities: [...findOthers.trendsEntities, ...result.ret.trendsPersonHomeList.trendsEntities]} });


                }
            }
            return result;
        },
        //zxl
        *othersHomeEFF({obj}, {put, call,select}) {
            const findOthers = yield select((state)=>state.findOthers);
            const result = yield call(othersHome, {pageNo: findOthers.pageNo, ...obj});
            if (result.code == 200) {
                yield put({type: 'save', data: {personHome: result.ret}});
                if (result.ret == "") {
                    yield put({type: 'save', data: {trendsEntities: []}})
                } else {
                    // if(result.pageNo <= 1) yield put({ type: 'save', data: {...result.ret, list: result.ret.list,pageSize:result.pageSize,pageNo:result.pageNo} });
                    if (result.ret.trendsPersonHomeList.pageNo <= 1) yield put({
                        type: 'save',
                        data: {personHome: result.ret, trendsEntities: result.ret.trendsList}
                    });

                    else yield put({
                        type: 'save',
                        data: {
                            personHome: result.ret,
                            trendsEntities: [...findOthers.trendsEntities, ...result.ret.trendsList]
                        }
                    });
                }
            }
            return result;
        },
        *findSerachDetailEFF({obj}, {put, call,select}) {
            const findOthers = yield select((state)=>state.findOthers);
            const result = yield call(findSerachDetail, { keyWord:findOthers.keyWord,trendsType:findOthers.trendsType, pageNo:findOthers.pageNo, ...obj });
            if (result.code == 200) {
                yield put({ type: 'save', data: result.ret  });
                if(result.ret==""){yield put({ type: 'save', data: { findSerachDetailList: []} })}else{
                    // if(result.pageNo <= 1) yield put({ type: 'save', data: {...result.ret, list: result.ret.list,pageSize:result.pageSize,pageNo:result.pageNo} });
                    if(result.ret.pageNo <= 1) yield put({ type: 'save', data: {...result.ret, findSerachDetailList: result.ret.list} });

                    else yield put({ type: 'save', data: { ...result.ret,findSerachDetailList: [...findOthers.findSerachDetailList, ...result.ret.list]} });
                    return { r:true };
                }
            }
        },

        *findSerachUserDetailEFF({obj}, {put, call,select}) {
            const findOthers = yield select((state)=>state.findOthers);
            const result = yield call(findSerachUserDetail, { keyWord:findOthers.keyWord, pageNo:findOthers.pageNo, ...obj });
            if (result.code == 200) {
                yield put({ type: 'save', data: result.ret  });
                if(result.ret==""){yield put({ type: 'save', data: { findSerachUserDetailList: []} })}else{
                    // if(result.pageNo <= 1) yield put({ type: 'save', data: {...result.ret, list: result.ret.list,pageSize:result.pageSize,pageNo:result.pageNo} });
                    if(result.ret.pageNo <= 1) yield put({ type: 'save', data: {...result.ret, findSerachUserDetailList: result.ret.list} });

                    else yield put({ type: 'save', data: { ...result.ret,findSerachUserDetailList: [...findOthers.findSerachUserDetailList, ...result.ret.list]} });

                }
            }
            return result;
        },
        *publishFindEFF({obj}, {put, call}) {
            // Toast.loading('加载中...', 3);
            const result = yield call(publishFind,obj);
            return result;
        },
        *delOneSelfEFF({obj}, {put, call}) {
            // Toast.loading('加载中...', 3);
            const result = yield call(delOneSelf,obj);
            return result;
        },
        *fastFindEFF({}, {put, call}) {
            // Toast.loading('加载中...', 3);
            const result = yield call(fastFind);
            if (result.code == 200) {
                yield put({type: 'save', data: {isFastFind:result.ret.isFastFind}});
            }
            return result;
        },

        *connerLikeEFF({ obj }, { put, call }) {
            const result = yield call(connerLike, obj);
            if (result.code == 200) {
                Toast.success(result.msg,1.5)
            } else {

            }
            return result
        },
        *connerFollowEFF({ obj }, { put, call }) {
            const result = yield call(connerFollow, obj);
            if (result.code == 200) {
                Toast.success(result.msg,1.5)
            } else {

            }
            return result
        },
        *getSelPeopleEFF({obj}, {put, call}) {
            // Toast.loading('加载中...', 3);
            const result = yield call(selPeopleApi, obj);
            if (result.code == 200) {
                return result
            } else {

            }
            return {a: true}
        },
        *getConListEFF({ obj }, { put, call, select }) {

            const findOthers = yield select((state)=>state.findOthers);
            const result = yield call(conListApi, {  pageNo:findOthers.pageNo, ...obj });
            if (result.code == 200) {
                yield put({ type: 'save', data: result.ret  });
                // if(result.pageNo <= 1) yield put({ type: 'save', data: {...result.ret, list: result.ret.list,pageSize:result.pageSize,pageNo:result.pageNo} });
                if(result.ret.pageNo <= 1) yield put({ type: 'save', data: {...result.ret, trendsConnerEntities: result.ret.list} });

                else yield put({ type: 'save', data: { ...result.ret,trendsConnerEntities: [...findOthers.trendsConnerEntities, ...result.ret.list] } });

                return { r:true };

            } else {

            }
        },
        *myFansListEFF({ obj }, { put, call, select }) {

            const findOthers = yield select((state)=>state.findOthers);
            const result = yield call(myFansListApi, {  pageNo:findOthers.pageNo, ...obj });
            if (result.code == 200) {
                yield put({ type: 'save', data: result.ret  });
                // if(result.pageNo <= 1) yield put({ type: 'save', data: {...result.ret, list: result.ret.list,pageSize:result.pageSize,pageNo:result.pageNo} });
                if(result.ret.pageNo <= 1) yield put({ type: 'save', data: {...result.ret, trendsFansEntities : result.ret.list} });

                else yield put({ type: 'save', data: { ...result.ret,trendsFansEntities: [...findOthers.trendsFansEntities, ...result.ret.list] } });

                return { r:true };

            } else {

            }
        },
        //获取热门话题
        *getTalkListEFF({obj}, {put, call,select}) {
            const findOthers = yield select((state)=>state.findOthers);
            const result = yield call(talkListApi, {  pageNo:findOthers.pageNo,topicName:findOthers.topicName, ...obj });
            if (result.code == 200) {
                yield put({ type: 'save', data: result.ret  });
                // if(result.pageNo <= 1) yield put({ type: 'save', data: {...result.ret, list: result.ret.list,pageSize:result.pageSize,pageNo:result.pageNo} });
                if(result.ret.pageNo <= 1) yield put({ type: 'save', data: {...result.ret, trendsTopicEntities: result.ret.list} });
                else yield put({ type: 'save', data: { ...result.ret,trendsTopicEntities: [...findOthers.trendsTopicEntities, ...result.ret.list] } });
                return { r:true };
            }
        },

        *submitSommentEFF({obj}, {put, call}) {
            // Toast.loading('加载中...', 3);
            const result = yield call(submitSomment, obj);
            return result;
            //if (result.code == 200) {
            //    yield put({type: 'save', data: {talkList:result.ret.trendsTopicEntities}});
            //    // console.log(conList.msg)
            //} else {
            //
            //}
        },
        *delSommentEFF({obj}, {put, call}) {
            // Toast.loading('加载中...', 3);
            const result = yield call(delSomment, obj);
            return result;
            //if (result.code == 200) {
            //    yield put({type: 'save', data: {talkList:result.ret.trendsTopicEntities}});
            //    // console.log(conList.msg)
            //} else {
            //
            //}
        },
        *commentLikeEFF({ obj }, { put, call, select }) {
            const result = yield call(commentLike, obj);
            if (result.code == 200) {
                Toast.success(result.msg,1.5)
            } else {

            }
            return result
        },
        *findCommentListEFF({obj}, {put, call, select}){
            //zxl
            const result = yield call(findCommentList, obj);
            const findOthers = yield select((state)=>state.findOthers);
            if (result.code == 200) {
                if(result.ret.pageNo == 1 || result.ret.pageNo == 0) yield put({type: 'save', data: {commentList:result.ret.list,commentListPageNo:result.ret.pageNo,commentTotalRows:result.ret.totalRows}});
                else yield put({type: 'save', data: { commentList:[...findOthers.commentList,...result.ret.list],commentListPageNo:result.ret.pageNo,commentTotalRows:result.ret.totalRows}});
                //后台接口字段totalRows，并不是总条数
            } else {
                Toast.fail(result.msg, 1.5);
            }
            return result

        },
        *commentDetailEFF({obj}, {put, call, select}){
            //zxl
            const result = yield call(commentDetail, obj);
            const findOthers = yield select((state)=>state.findOthers);
            if (result.code == 200) {
                var newData = findOthers.commentList.map(v => {
                    if (v.commentsId == obj.id) {

                        if (parseInt(result.ret.totalRedict)<10){
                            v.isShowMore=true
                        }else {
                            v.isShowMore=false
                            v.trendsSecondCommentEntityPageNo=parseInt(result.ret.pageNo)+1;
                        }
                        v.commentTotalRows=result.ret.totalRows;
                        if (obj.pageNo==1){
                            v.trendsSecondCommentEntity = result.ret.list;
                        }else {
                            v.trendsSecondCommentEntity = [...v.trendsSecondCommentEntity, ...result.ret.list];
                        }
                    }
                    return v;
                })
                yield put({type: 'save', data: {commentList: newData}})
            } else {
                Toast.fail(result.msg, 1.5);
            }
            return result

        },
        *myArticleApiEFF({obj}, {put, call, select}){
            //zxl
            const result = yield call(myArticleApi, obj);
            const findOthers = yield select((state)=>state.findOthers);
            if (result.code == 200) {
               if(parseInt(obj.pageNo)==1) yield put({type: 'save', data: {articleList:result.ret.list,pageNo:result.ret.pageNo,totalRows:result.ret.totalRows}});
               else yield put({type: 'save', data: { articleList:[...findOthers.articleList,...result.ret.articleList],pageNo:result.ret.pageNo,totalRows:result.ret.totalRows}});
            } else {
                Toast.fail(result.msg, 1.5);
            }
            return result

        },
        subscriptions: {
            setup({ dispatch, history }) {
                return history.listen(({ pathname, query }) => {
                    if (pathname == '/findOthers') {
                        dispatch({ type: 'getStylesEFF',obj:{pageNo:1} });
                    }
                    if(pathname.includes("/findIndex")||pathname == '/personHome'||pathname.includes("/topicPage")||pathname.includes("/findSearch")||pathname.includes("/publish")){
                        dispatch({type:"clearState"})
                    }
                    if(pathname.includes('/findDetail/')){
                        const match = pathToRegexp('/findDetail/:data/:scroll').exec(pathname);
                        dispatch({ type:'findDetailsEFF', obj:{ id:match[1]} });
                    }
                });
            },
        }
    },

}